package agents;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.myprofile.profile.MuseumItem;
import com.myprofile.profile.ProfileType;

import daiia.ProfileManager;

import model.Proposal;
import model.TourGuide;
import model.TourItem;
import behaviours.RegisterServiceBehaviour;
import behaviours.TourNegotiationBehaviour;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.ServiceDescription;

import model.Tour;

public class TourGuideAgent extends Agent implements TourGuide{

	private static final long serialVersionUID = 2732959643354920009L;
	
	private ArrayList<Proposal> finishedProposals;
	
	@Override
	protected void setup() {
		
		finishedProposals = new ArrayList<Proposal>();
		
		// Register with DF
		ServiceDescription sd = new ServiceDescription();
		sd.setType("tour-creation");
		sd.setName("touragent");
		sd.addProperties(new Property("Profile", null));
		ServiceDescription[] sds = new ServiceDescription[1];
		sds[0] = sd;
		addBehaviour(new RegisterServiceBehaviour(getAID(), sds));
		
		// Utility HashMap
		HashMap<String, Integer> utilities = new HashMap<String, Integer>();
		utilities.put("P1", 30);
		utilities.put("P2", 45);
		utilities.put("P3", 0); // TODO Add N here
		utilities.put("T1", 30);
		utilities.put("T2", 75);
		utilities.put("T3", 90);
		
		// Proposal ArrayList
		ArrayList<Proposal> proposals = new ArrayList<Proposal>();
		generateProposals(proposals);
		
		addBehaviour(new TourNegotiationBehaviour(utilities, proposals, false, null, this, finishedProposals));
	}

	private void generateProposals(ArrayList<Proposal> proposals) {
		
		String[] buyItems1 = {"P1", "P2", "P3"};
		String[] sellItems1 = {"T1"};
		proposals.add(new Proposal(buyItems1, sellItems1, 0));
		
		String[] buyItems2 = {"P2", "P3"};
		String[] sellItems2 = {"T1"};
		proposals.add(new Proposal(buyItems2, sellItems2, 0));
		
		String[] buyItems3 = {"P1", "P3"};
		String[] sellItems3 = {"T1"};
		proposals.add(new Proposal(buyItems3, sellItems3, 0));
		
		String[] buyItems4 = {"P1", "P2", "P3"};
		String[] sellItems4 = {"T2"};
		proposals.add(new Proposal(buyItems4, sellItems4, 0));
		
		String[] buyItems5 = {"P2"};
		String[] sellItems5 = {"T1"};
		proposals.add(new Proposal(buyItems5, sellItems5, 0));
		
		String[] buyItems6 = {"P1", "P2", "P3"};
		String[] sellItems6 = {"T3"};
		proposals.add(new Proposal(buyItems6, sellItems6, 0));
		
		String[] buyItems7 = {"P1", "P2", "P3"};
		String[] sellItems7 = {"T1", "T2"};
		proposals.add(new Proposal(buyItems7, sellItems7, 0));
		
		String[] buyItems8 = {"P3"};
		String[] sellItems8 = {"T1"};
		proposals.add(new Proposal(buyItems8, sellItems8, 0));
		
		String[] buyItems9 = {"P1"};
		String[] sellItems9 = {"T1"};
		proposals.add(new Proposal(buyItems9, sellItems9, 0));
		
		String[] buyItems10 = {"P1", "P2"};
		String[] sellItems10 = {"T2"};
		proposals.add(new Proposal(buyItems10, sellItems10, 0));
	}

	@Override
	public Tour getTour(String id, String attitude) {
		
		if(attitude.equals("normal")) {
			return loadTour("src/tours/Normal"+id+".xml", id);
		} else {
			return loadTour("src/tours/Greedy"+id+".xml", id);
		}
	}
	
	private Tour loadTour(String path, String id) {
		ProfileManager pm = new ProfileManager();
		ProfileType profTour = pm.loadProfile(path);
		
		List<MuseumItem> items = profTour.getVisitedItems().getVisitedItem();
		
		Tour tour = new Tour(id);
		
		for(MuseumItem item : items) {
			tour.addTourItem(new TourItem(item.getId(), item.getName()));
		}
		
		return tour;
	}
}
